package com.ksd.common.utils.sign;

import java.math.BigInteger;

public class BytesUtils {
    public static String ByteToHexString(byte[] byteArray) {
        if(byteArray == null || byteArray.length <= 0) {
            return null;
        }
        StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < byteArray.length; i++) {
            if ((byteArray[i] & 0xff) < 0x10)
                hexString.append("0");
            hexString.append(Integer.toHexString(0xFF & byteArray[i]));
        }
        return hexString.toString().toLowerCase();
    }

    public static byte[] HexStringToByte(String hexString) {
        byte[] byteArray = null;
        int k = 0;
        if (hexString == null || hexString.isEmpty()) {
            return null;
        }
        if (hexString.length() % 2 != 0) {
            return null;
        }
        hexString = hexString.toLowerCase();
        byteArray = new byte[hexString.length() / 2];
        for (int i = 0; i < byteArray.length; i++) {
            byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
            byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
            byteArray[i] = (byte) (high << 4 | low);
            k += 2;
        }
        return byteArray;
    }

    public static byte[] asUnsigned32ByteArray(BigInteger value) {
        byte[] out = asUnsignedByteArray(value);
        if (out.length == 32)
            return out;
        if (out.length < 32) {
            byte[] buf = new byte[32];
            System.arraycopy(out, 0, buf, 32 - out.length, out.length);
            return buf;
        }
        throw new RuntimeException("参数有误");
    }

    public static byte[] asUnsignedByteArray(BigInteger value) {
        byte[] bytes = value.toByteArray();

        if (bytes[0] == 0) {
            byte[] tmp = new byte[bytes.length - 1];

            System.arraycopy(bytes, 1, tmp, 0, tmp.length);

            return tmp;
        }

        return bytes;
    }

    public static int byteToInt(byte[] bytes)
    {
        int num = 0;
        int temp;
        temp = (0x000000ff & (bytes[0])) << 0;
        num = num | temp;
        temp = (0x000000ff & (bytes[1])) << 8;
        num = num | temp;
        temp = (0x000000ff & (bytes[2])) << 16;
        num = num | temp;
        temp = (0x000000ff & (bytes[3])) << 24;
        num = num | temp;
        return num;
    }

    public static int byteToInt(byte[] value, int index) {
        int ret = 0;
        for(int i = 0; i < 4; i++) {
            ret = ret << 8 | (value[index+i] & 0xff);
        }
        return ret;
    }

    public static int bytesToInt(byte[] src, int offset) {
        int value;
        value = (int) ((src[offset] & 0xFF)
                | ((src[offset+1] & 0xFF)<<8)
                | ((src[offset+2] & 0xFF)<<16)
                | ((src[offset+3] & 0xFF)<<24));
        return value;
    }

    public static byte[] intToBytes(int num)
    {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) (0xff & (num >> 0));
        bytes[1] = (byte) (0xff & (num >> 8));
        bytes[2] = (byte) (0xff & (num >> 16));
        bytes[3] = (byte) (0xff & (num >> 24));
        return bytes;
    }

    public static byte[] longToBytes(long num)
    {
        byte[] bytes = new byte[8];
        for (int i = 0; i < 8; i++)
        {
            bytes[i] = (byte) (0xff & (num >> (i * 8)));
        }

        return bytes;
    }

    public static String byteFormat(byte[] srcData) {
        if(srcData == null || srcData.length <= 0) {
            return null;
        }
        StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < srcData.length; i++) {
            if(i == 0) {
                hexString.append("\r\n");
            }
            hexString.append("0x");
            if ((srcData[i] & 0xff) < 0x10)
                hexString.append("0");
            hexString.append(Integer.toHexString(0xFF & srcData[i]) + ",");
            if((i + 1) % 16 == 0) {
                hexString.append("\r\n");
            }
        }
        hexString.append("\r\n");
        return hexString.toString().toLowerCase();
    }

    public static byte[] intToBytes(int value, int retbuflen) {
        byte[] result = new byte[retbuflen];
        result[0] = (byte)(value >> 24);
        result[1] = (byte)(value >> 16);
        result[2] = (byte)(value >> 8);
        result[3] = (byte)(value);
        return result;
    }

    public static byte[] longToByte(long value, int retbuflen) {
        byte[] result = new byte[retbuflen];
        result[0] = (byte)((value >> 56) & 0xff);
        result[1] = (byte)((value >> 48) & 0xff);
        result[2] = (byte)((value >> 40) & 0xff);
        result[3] = (byte)((value >> 32) & 0xff);
        result[4] = (byte)((value >> 24) & 0xff);
        result[5] = (byte)((value >> 16) & 0xff);
        result[6] = (byte)((value >> 8) & 0xff);
        result[7] = (byte)((value) & 0xff);
        return result;
    }

    public static byte[] shortToByte(short value, int retbuflen) {
        byte[] result = new byte[retbuflen];
        result[0] = (byte)((value >> 8) & 0xff);
        result[1] = (byte)((value) & 0xff);
        return result;
    }

    public static String byteFormat(byte srcData) {
        return "0x" + Integer.toHexString(0xFF & srcData);
    }
}